﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TpsAutoSurvey_PDA_Version_.Presenter;
using System.Threading;


namespace TpsAutoSurvey_PDA_Version_.RealizedClasses
{
    public class TimerClass
    {

        public List<DateTime> invokeTimes;
        public Timer timer;
        private bool iscustomtimer = false;


        public bool IsCustomTimer
        {
            get
            {
                return this.iscustomtimer;
            }
            set
            {

                if (value == iscustomtimer)
                {
                    return;
                }
                iscustomtimer = value;
                if (iscustomtimer == true)
                {
                    this.Interval = 1000 * 60;
                    //timer.Change(1000 * 60, 1000 * 60);
                }
                else
                {
                    this.Interval = 1000 * 3600;
                    //timer.Change(TimeSpan.FromHours(1).Milliseconds, TimeSpan.FromHours(1).Milliseconds);
                }
              
            }
        }
        private int interval;
        public int Interval
        {
            get
            {
                return interval;
            }
            set
            {
                interval = value;
                timer.Change(interval, interval);
            }
        }

        public event EventHandler SurveyTimeup;
            


        public TimerClass()
        {
            SetupTimer();
        }
        void SetupTimer()
        {
            bool is_c = false;
            timer = new Timer(new TimerCallback(timer_Tick), null, 0, 1000 * 3600);         
            this.Interval = (int)(ToolClass.ReadTimeInterval(out is_c) * 1000 * 3600); 
            this.IsCustomTimer = is_c;
            this.invokeTimes = ToolClass.ReadTimerList();

           
        }
        void timer_Tick(object sender)
        {
            try
            {
                if (IsCustomTimer == false)
                {
                    if (SurveyTimeup != null)
                    {
                        SurveyTimeup(this, null);
                    }
                }
                else
                {
                    foreach (DateTime t in this.invokeTimes)
                    {
                        if (ShouldTimerInvoke(t))
                        {
                            if (SurveyTimeup != null)
                            {
                                SurveyTimeup(this, null);
                            }
                        }
                    }
                }
            }
            catch(Exception a)
            {
                ToolClass.Log(a.Message);
            }
        }
        public void AddTime(DateTime time)
        {
            invokeTimes.Add(time);
        }
        public void Remove(DateTime time)
        {
            
            invokeTimes.RemoveAll((p) => { return (p.Hour == time.Hour && p.Minute == time.Minute); });
        }

        private bool ShouldTimerInvoke(DateTime time)
        {
            if (DateTime.Now.Hour == time.Hour && DateTime.Now.Minute == time.Minute)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
}
